<?php

namespace zhtfrontend\modules\cardmanage\controllers;

use common\components\helpers\Fun;
use common\models\datas\CardAttr;
use common\models\datas\CardReSend;
use common\models\datas\CardSend;
use common\models\datas\CardsIssuerinfor;
use common\models\datas\IsuserInfo;
use yii\helpers\ArrayHelper;
use zhtfrontend\modules\cardmanage\components\rest\ActiveController;
use yii\web\Response;

class AccountController extends ActiveController
{
    public $modelClass = 'common\models\datas\IsuserInfo';

    public $serializer = [
        'class' => 'zhtfrontend\modules\cardmanage\components\rest\Serializer',
        'collectionEnvelope' => 'items',
    ];

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
        return $behaviors;
    }
    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'], $actions['create'], $actions['update'], $actions['delete'], $actions['view']);
        return $actions;
    }
    public function actionUpdate(){
        $data = \Yii::$app->request->get();
        $old_password=ArrayHelper::keyExists('old_password', $data) ? $data['old_password'] : '';
        $new_passowrd=ArrayHelper::keyExists('new_password', $data) ? $data['new_password'] : '';
        $old_password_hash=\Yii::$app->getSecurity()->generatePasswordHash($old_password);
        $user_id=ArrayHelper::keyExists('user_id', $data) ? $data['user_id'] : '';
        if ($user_id!==''){
            $user=CardsIssuerinfor::findOne($user_id);
            if (\Yii::$app->getSecurity()->validatePassword($old_password, $user->password)) {
                $user->password=\Yii::$app->getSecurity()->generatePasswordHash($new_passowrd);
                if ($user->save(false)){
                    return ['items'=>$user];
                }else{
                    return ['statusCode' =>202, 'message' => 'error', 'result' =>'保存失败'];
                }

            }else{
                return ['statusCode' =>201, 'message' => 'error', 'result' =>'原密码错误'];
            }

        }else{
            return ['statusCode' =>0, 'message' => 'error', 'result' =>'用户为空'];
        }
    }
    public function actionSms(){

    }
}